<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>范闲商城-用户中心</title>
	<link rel="stylesheet" type="text/css" th:href="@{/static/css/reset.css}">
	<link rel="stylesheet" type="text/css" th:href="@{/static/css/main.css}">
	<link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
	<script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
	<script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
	<script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
	<style>
		.avatar-img {
			width: 20px; /* 设置头像图片的宽度 */
			height: 20px; /* 设置头像图片的高度，可以根据需要调整 */
			border-radius: 50%; /* 使图片呈现圆形 */
			margin-right: 10px; /* 设置头像图片与用户名之间的间距，可以根据需要调整 */
		}
		.site_con {
			display: flex;
			justify-content: space-between; /* 分隔表单和按钮 */
			align-items: start; /* 对齐到顶部 */
		}

		.button-group {
			display: flex;
			flex-direction: column;
			align-items: center; /* 水平居中按钮 */
			margin-right: 50px; /* 距离右侧边缘50px */
		}

	</style>
</head>
<body>
	<div th:replace="header::top"></div>


	<div class="search_bar clearfix">
		<a href="/" class="logo fl"><img src="/static/pic/fanxian-mall.png" width="150px" height="60px"></a>
		<div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;用户中心</div>
		<div class="search_con fr">
			<input type="text" id="searchInput" class="input_text fl" placeholder="搜索商品">
			<input type="button" id="searchButton" class="input_btn fr" value="搜索">
		</div>
	</div>

	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部商品分类</h1>
				<span></span>
				<ul class="subnav" id="LAY_CategoryList">
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="/">首页</a></li>
				<li class="interval">|</li>
				<li><a href="/page/list">全部商品</a></li>
				<li class="interval">|</li>
				<li><a href="/page/turntable">抽奖</a></li>
			</ul>
		</div>
	</div>
	<br>

	<div class="main_con clearfix">
		<div class="left_menu_con clearfix">
			<h3>用户中心</h3>
			<ul>
				<li><a href="/page/user/info">· 个人信息</a></li>
				<li><a href="/page/user/order">· 全部订单</a></li>
				<li><a href="/page/user/site">· 收货地址</a></li>
				<li><a href="/page/user/coupon">· 我的优惠券</a></li>
				<li><a href="/page/user/collect">· 我的收藏</a></li>
				<li><a href="/page/user/footprint" class="active">· 我的足迹</a></li>
			</ul>
		</div>
		<div class="right_content clearfix" id="footprintList">
			<div>
				<h3 class="common_title2">我的足迹</h3>
				<div class="has_view_list">
					<ul class="goods_type_list clearfix" id="footprintListByDay">
						<li>
							<a href="detail.html"><img src="/static/images/goods/goods003.jpg"></a>
							<h4><a href="detail.html">大兴大棚草莓</a></h4>
							<div class="operate">
								<span class="prize">￥16.80</span>
								<span class="unit">16.80/500g</span>
								<a href="#" class="add_goods" title="加入购物车"></a>
							</div>
						</li>
					</ul>
				</div>
			</div>


		</div>
	</div>



	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2024 北京鑫鑫科技有限公司 All Rights Reserved</p>
		<p>电话：010-8888888    京ICP备88888888号</p>
	</div>
	<script>
		$('#searchButton').click(function() {
			var searchText = $('#searchInput').val(); // 获取搜索框中的值
			var searchUrl = '/page/list?search=' + encodeURIComponent(searchText); // 构建带有搜索关键字的URL
			window.location.href = searchUrl; // 跳转到另一个页面
		});

		$.post(
				'/footprint/listFootprint',
				function (result) {
					if (result.code == 0) {
						var html = ''
						$('#footprintList').empty();
						$(result.data).each(function () {
							var currentDate = new Date();
							var year = currentDate.getFullYear();
							var month = currentDate.getMonth() + 1;
							var day = currentDate.getDate();
							// 在单个数字前添加零，以保持日期格式的一致性
							if (month < 10) {
								month = "0" + month;
							}
							if (day < 10) {
								day = "0" + day;
							}
							var dateString = year + "-" + month + "-" + day;
							if (this.createTime == dateString) {
								html += '<div>'
								html += '	<h3 class="common_title2">今天</h3>'
								html += '	<div class="has_view_list">'
								html += '		<ul class="goods_type_list clearfix">'
								$(this.list).each(function () {
									html += '			<li id="'+this.id+'" data-product-id="' + this.productId + '">'
									html += '				<a href="/page/detail?productId=' + this.productId + '&parentId=' + this.parentId + '&categoryId=' + this.categoryId + '"><img src="' + this.productImage + '"></a>'
									html += '				<h4><a href="/page/detail?productId=' + this.productId + '&parentId=' + this.parentId + '&categoryId=' + this.categoryId + '">' + this.productName + '</a></h4>'
									html += '				<div class="operate">'
									html += '					<span class="prize">￥' + this.price + '</span>'
									html += '					<span class="unit"><i class="layui-icon layui-icon-delete" id="delete"></i> </span>'
									html += '					<a href="javascript:;" class="add_goods" title="加入购物车" id="add_cart"></a>'
									html += '				</div>'
									html += '			</li>'
								})
								html += '		</ul>'
								html += '	</div>'
								html += '</div>'
							} else {
								html += '<div >'
								html += '	<h3 class="common_title2">' + this.createTime + '</h3>'
								html += '	<div class="has_view_list">'
								html += '		<ul class="goods_type_list clearfix">'
								$(this.list).each(function () {
									html += '			<li id="'+this.id+'" data-product-id="' + this.productId + '">'
									html += '				<a href="/page/detail?productId=' + this.productId + '&parentId=' + this.parentId + '&categoryId=' + this.categoryId + '"><img src="' + this.productImage + '"></a>'
									html += '				<h4><a href="/page/detail?productId=' + this.productId + '&parentId=' + this.parentId + '&categoryId=' + this.categoryId + '">' + this.productName + '</a></h4>'
									html += '				<div class="operate">'
									html += '					<span class="prize">￥' + this.price + '</span>'
									html += '					<span class="unit"><i class="layui-icon layui-icon-delete" id="delete"></i> </span>'
									html += '					<a href="javascript:;" class="add_goods" title="加入购物车" id="add_cart"></a>'
									html += '				</div>'
									html += '			</li>'
								})
								html += '		</ul>'
								html += '	</div>'
								html += '</div>'
							}
						}), 
						$('#footprintList').append(html);
						$('#footprintList').on('click', '#add_cart', function () {
							//加入购物车之前首先判断有没有登录
							//如果没有登录首先完成登录
							//如果已经登录，就将商品信息插入到cart购物车表里面，然后跳转到购物车列表界面
							var productId = $(this).closest('li').attr('data-product-id');
							$.post(
									'/product/checkStock',
									{ 'id': productId },
									function (result) {
										if (result.code == 0) {
											//代表已经登录，直接插入购物车
											addToCart(productId);
										} else {
											mylayer.errorMsg(result.msg);
										}
									},
									'json'
							);
						});
						function addToCart(productId) {
							console.log('addToCart');
							$.ajax({
								url: '/cart/add',
								type: 'POST',
								data: {'productId': productId, 'quantity': 1},
								dataType: 'json',
								success: function (result) {
									if (result.code == 0) {
										mylayer.okMsg(result.msg);
									} else {
										mylayer.errorMsg(result.msg);
									}
								},
								complete : function(xhr, status) {
									//拦截器实现超时跳转到登录页面
									// 通过xhr取得响应头
									var REDIRECT = xhr.getResponseHeader("REDIRECT");
									//如果响应头中包含 REDIRECT 则说明是拦截器返回的需要重定向的请求
									if (REDIRECT == "REDIRECT") {
										var win = window;
										while (win != win.top) {
											win = win.top;
										}
										win.location.href = xhr.getResponseHeader("CONTEXTPATH");
									}
								}
							});
						}
						$('#footprintList').on('click', '#delete',function() {

							var id = $(this).closest('li').attr('id');

							layer.confirm(
									'您确定要删除吗？',
									{icon : 3},
									function (index) {
										$.post(
												'/footprint/delete',
												{'id': id},
												function (result) {
													if (result.code == 0) {
														mylayer.okMsg(result.msg);
														setInterval(function() {
															window.location.reload();
														}, 2000)
													} else {
														mylayer.errorMsg(result.msg);
													}
												},
												'json'
										);
									}
							)

						});
					}
				},
				'json'
		);


		$.post(
				'/category/listAll',
				function (result) {
					if (result.code == 0) {
						$('#LAY_CategoryList').empty();
						$(result.data).each(function () {
							$('#LAY_CategoryList').append('<li><a href="/page/list?parentId=' + this.id + '">'+this.name+'</a></li>');
						})
					}
				},
				'json'
		);


		function logout() {
			layer.confirm(
					'您确认要退出么',
					{icon:3},
					function() {
						$.post(
								'/user/logout',
								function (result) {
									if (result.code == 0) {
										mylayer.okMsg(result.msg);
										setTimeout(function() {
											window.location.reload();
										}, 800);
									}
								},
								'json'
						);
						// location.href = '/admin?method=logout'
					}
			);
		}
	</script>
</body>
</html>